package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author stone
 * @date 2022/06/23 17:24
 */
@ControllerAdvice
public class CustomExceptionControllerAdvice {

    //@ExceptionHandler(value = {ArithmeticException.class})
    // 返回了ModelAndView
    /*@ExceptionHandler(ArithmeticException.class)
    public ModelAndView method1() {
        return new ModelAndView("/exception.jsp");
    }*/

    // 返回了ModelAndView → 返回值字符串作为ModelAndView中的viewName
    /*@ExceptionHandler(ArithmeticException.class)
    public String method1() {
        return "/exception.jsp";
    }*/

    // 在形参中可以直接接收你抛出的异常，你映射的是什么异常，就可以以什么类型的形参来接收
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public BaseRespVo method1(ArithmeticException exception) {
        return BaseRespVo.fail(exception.getMessage());
    }

}
